package org.zn.note.jdk.others;

/**
 * @ClassName FangDai
 * @Author zn
 * @Date 2019/10/8 13:14
 **/
public class FangDai {

//    private final static double PRINCIPAL = 350000.0;
    private final static double PRINCIPAL = 2750000.0;
    // 折扣：85折
    private final static double DISCOUNT = 0.85;
//    private final static double DISCOUNT = 1.0;
    // 年利率：4.95%
    private final static double RATE_YEAR = 4.9 / 100.0 * DISCOUNT;
    // 月利率
    private final static double RATE_MONTH = RATE_YEAR / 12.0;
    // 贷款年限：30年
    private final static double YEARS = 30;
    // 还款月数
    private final static double MONTHS = 12.0 * YEARS;

    public static void main(String[] args) {
        double monthAmount = monthAmount(PRINCIPAL, RATE_MONTH, MONTHS);
        println("实际年利率：" + RATE_YEAR + ", 实际月利率：" + RATE_MONTH + ", 每月还款：" + monthAmount);
        // 当月利息
        double interest = 0.0;
        // 当月还款本金
        double repayment = 0.0;
        // 剩余代还款本金
        double remaining = PRINCIPAL;
        // 累计利息
        double interestSum = 0.0;

        for (int i = 1; i <= MONTHS; i++) {
            interest = remaining * RATE_MONTH;
            repayment = monthAmount - interest;
            remaining = remaining - repayment;
            interestSum += interest;
            println("第" + i + "月：利息[" + interest + "], 本金[" + repayment + "], 剩余本金[" + remaining + "]");
        }
        println("总利息：" + interestSum);
    }

    private static double monthAmount(double principal, double monthRate, double months) {
        /*
        等额本息
        P 贷款本金
        R 月利息
        N 还款期数
        每月还款 = P * (R * (1+R)^N) / ((1+R)^N - 1)
         */
        return principal * (monthRate * Math.pow(1 + monthRate, months)) / (Math.pow(1 + monthRate, months) - 1);
    }

    private static void println(String s) {
        System.out.println(s);
    }
}
